/*
 * Created on 2005/4/12
 *
 */
package com.erp.app.report.genericInquiry;

import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import com.erp.app.common.ProjectConstants;
import com.erp.app.report.genericInquiry.inquiryCriteria.beans.vo.InquiryCriteriaVO;
import com.erp.app.report.genericInquiry.inquiryMaster.beans.GenericInquiryMasterVO;
import com.erp.app.util.web.WebUtil;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;
import com.erp.framework.util.user.User;


public class GenericInquiryUtil {
    public static String getDataAccessSecurityWhereClause(HttpSession session) {
        return "";
    }
    
    public static String parseQueryString(GenericInquirySearchForm form, HttpSession session, String query, int index) {
        GenericInquiryMasterVO genericInquiryMasterVO = form.getGenericInquiryMasterVO();
        String[] srchFrom = form.getSrchFrom();
        
        ArrayList critList = genericInquiryMasterVO.getLocalChilds(InquiryCriteriaVO.class);
        InquiryCriteriaVO vo = (InquiryCriteriaVO)critList.get(index); 
        
        if (CommonUtil.isEmpty(query))
            return "";
        
        StringBuffer rslt = new StringBuffer(query);
        int idx = 0;
        int eIdx = 0;
        
        while (idx != -1) {
            query = rslt.toString();
            eIdx = 0;
            idx = query.indexOf(GenericInquiryConstants.QUERYSTRING_PLACEHOLDER_COMPANY_CODE, eIdx); 
            if (idx > -1) {
                eIdx = query.indexOf(GenericInquiryConstants.QUERYSTRING_SUFFIX, idx) + GenericInquiryConstants.QUERYSTRING_SUFFIX.length();
                String companyCode = "'" + WebUtil.getAppropriateCompanyCode(session, vo.getDdShare()) + "'";
                rslt.replace(idx, eIdx, companyCode);
            }
        }
        
        idx = 0;
        eIdx = 0;
        
        while(idx != -1) {
            query = rslt.toString();
            eIdx = 0;
            idx = query.indexOf(GenericInquiryConstants.QUERYSTRING_PLACEHOLDER_DEPT, eIdx);
            if (idx > -1) {
                eIdx = query.indexOf(GenericInquiryConstants.QUERYSTRING_SUFFIX, idx) + GenericInquiryConstants.QUERYSTRING_SUFFIX.length();
                String depts = "'"+GenericInquiryUtil.getDataAccessSecurityWhereClause(session)+"'";
                rslt.replace(idx, eIdx, depts);
            }
        }
        
        idx = 0;
        eIdx = 0;
        
        while(idx != -1) {
            query = rslt.toString();
            eIdx = 0;
            idx = query.indexOf(GenericInquiryConstants.QUERYSTRING_PLACEHOLDER_USER, eIdx);
            if (idx > -1) {
                eIdx = query.indexOf(GenericInquiryConstants.QUERYSTRING_SUFFIX, idx) + GenericInquiryConstants.QUERYSTRING_SUFFIX.length();
                User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
                rslt.replace(idx, eIdx, "'"+editingUser.getUserId()+"'");
            }
        }
        
        idx = 0;
        eIdx = 0;
        
        while(idx != -1) {
            query = rslt.toString();
            eIdx = 0;
            idx = query.indexOf(GenericInquiryConstants.QUERYSTRING_PLACEHOLDER_CRITERIA_FIELD, eIdx);
            if (idx > -1) {
                eIdx = query.indexOf(GenericInquiryConstants.QUERYSTRING_SUFFIX, idx) + GenericInquiryConstants.QUERYSTRING_SUFFIX.length();
                
                int field_num;
                
                try {
                    field_num = Integer.parseInt(rslt.substring(idx + GenericInquiryConstants.QUERYSTRING_PLACEHOLDER_CRITERIA_FIELD.length(), eIdx - GenericInquiryConstants.QUERYSTRING_SUFFIX.length()));
                    rslt.replace(idx, eIdx, srchFrom == null ? "''" : "'" + srchFrom[field_num-1] + "'");
                } catch (NumberFormatException e) {
                   Logger.error("GenericInquiryUtil:Invalid CFIELD Index! Check your " 
                                + GenericInquiryConstants.QUERYSTRING_PREFIX 
                                + "CFIELD(line_no)"
                                + GenericInquiryConstants.QUERYSTRING_SUFFIX 
                                + " in your DD_WHERE.\n" + e);
                }
            }
        }
        
        return rslt.toString();
    }
}
